package library;

import java.util.HashSet;

import javax.annotation.Resource;
import javax.annotation.security.RolesAllowed;
import javax.ejb.EJBContext;
import javax.ejb.Stateless;
import javax.interceptor.Interceptors;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;

import library.IF.BookDAO;
import library.IF.CustomerDAO;
import library.IF.LibraryManagerIF;

import org.jboss.security.annotation.SecurityDomain;

/**
 * The Class LibraryManagerBean.
 * 
 * @author Pim Hazebroek
 */

@Stateless
//@Interceptors(library.Log.class)
@SecurityDomain("ABC-Library-Security-Domain")
@WebService(name = "ABC-Library", targetNamespace = "http://pimhazebroek.nl/ws", serviceName = "ABC-LibraryService")
public class LibraryManagerBean implements LibraryManagerIF {

	/** The context. */
	//@Resource
	//private EJBContext context = null;

	/**
	 * Instantiates a new library manager.
	 */
	public LibraryManagerBean() {
		System.out.println("LibraryManagerBean()");
	}

	@WebMethod
	@RolesAllowed( { "cashier", "administrator", "manager" })
	public HashSet<BookDAO> allBooks1() {
		return Book.allBooks();
	}

	@WebMethod
	@RolesAllowed( { "cashier", "administrator", "manager" })
	public HashSet<String> allBooks2(String name) throws Exception {
		Customer customer = Customer.findCustomer(name);
		if (customer != null) {
			return customer.getBooks();
		}
		throw new Exception("\"" + name + "\" does not exist in database");
	}

	@WebMethod
	@RolesAllowed( { "cashier", "administrator", "manager" })
	public HashSet<CustomerDAO> allCustomers() {
		return Customer.allCustomers();
	}

	@WebMethod
	@RolesAllowed( { "cashier", "administrator", "manager" })
	public String addCredits(String name, int credits) throws Exception {
		System.out.println("LibraryManagerBean.addCredits(" + name + ", " + credits + ")");
		Customer customer = Customer.findCustomer(name);
		if (customer != null) {
			int newCredits = credits + customer.getCredits();
			customer.setCredits(newCredits);
			return "\"" + name + "\" has " + newCredits + " credits";
		}
		throw new Exception("\"" + name + "\" not exist in database");
	}

	@WebMethod
	@RolesAllowed( { "cashier", "administrator", "manager" })
	public String lend(String title, String name) throws Exception {
		System.out.println("LibraryManagerBean.lend(" + title + ", " + name + ")");
		Book book = Book.findBook(title);
		if (book != null) {
			Customer lendTo = book.getLendTo();
			if (lendTo == null) {
				Customer customer = Customer.findCustomer(name);
				if (customer != null) {
					int credits = customer.getCredits();
					if (credits > 0) {
						customer.setCredits(--credits);
						customer.addBook(book);
						book.setLendTo(customer);
						return "\"" + title + "\" lend to " + name;
					}
					throw new Exception("\"" + name + "\" has no credits");
				}
				throw new Exception("\"" + name + "\" not exist in database");
			}
			throw new Exception("\"" + title + "\" is lend to " + lendTo.getName());
		}
		throw new Exception("\"" + title + "\" not exist in database");
	}

	@WebMethod
	@RolesAllowed( { "cashier", "administrator", "manager" })
	public String newBook(String title) throws Exception {
		System.out.println("LibraryManagerBean.newBook(" + title + ")");
		Book book = Book.findBook(title);
		if (book == null) {
			new Book(title);
			return "\"" + title + "\" put in database";
		}
		throw new Exception("\"" + title + "\" exist in database");
	}

	@WebMethod
	@RolesAllowed( { "cashier", "administrator", "manager" })
	public String newCustomer(String name) throws Exception {
		System.out.println("LibraryManagerBean.newCustomer(" + name + ")");
		Customer customer = Customer.findCustomer(name);
		if (customer == null) {
			new Customer(name);
			return "\"" + name + "\" put in database";
		}
		throw new Exception("\"" + name + "\" exist in database");
	}

	@WebMethod
	@RolesAllowed( { "cashier", "administrator", "manager" })
	public String removeBook(String title) throws Exception {
		System.out.println("LibraryManagerBean.removeBook(" + title + ")");
		Book book = Book.findBook(title);
		if (book != null) {
			Book.removeBook(title);
			return "\"" + title + "\" removed from database";
		}
		throw new Exception("\"" + title + "\" not exist in database");
	}

	@WebMethod
	@RolesAllowed( { "cashier", "administrator", "manager" })
	public String removeCustomer(String name) throws Exception {
		System.out.println("LibraryManagerBean.removeCustomer(" + name + ")");
		Customer customer = Customer.findCustomer(name);
		if (customer != null) {
			Customer.removeCustomer(name);
			return "\"" + name + "\" removed from database";
		}
		throw new Exception("\"" + name + "\" not exist in database");
	}

	@WebMethod
	@RolesAllowed( { "cashier", "administrator", "manager" })
	public String unlend(String title) throws Exception {
		System.out.println("LibraryManagerBean.unlend(" + title + ")");
		Book book = Book.findBook(title);
		if (book != null) {
			Customer customer = book.getLendTo();
			if (customer != null) {
				book.setLendTo(null);
				customer.removeBook(book);
				return "\"" + title + "\" is free";
			}
			throw new Exception("\"" + title + "\" was free");
		}
		throw new Exception("\"" + title + "\" not exist in database");
	}

}
